#include "widget.h"
#include "ui_widget.h"

#include <QRegExpValidator>
#include <QString>
#include <QDebug>
#include <QCheckBox>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 1.验证用户输入信息是否符合要求
    ui->pushButton->setEnabled(false);
    // 给lineEdit设置一个验证器
    ui->lineEdit->setValidator(new QRegExpValidator(QRegExp("^1\\d{10}$")));

    // 2.验证密码是否一致
    lineEdit1 = new QLineEdit(this);
    lineEdit2 = new QLineEdit(this);
    lineEdit1->setEchoMode(QLineEdit::Password);
    lineEdit2->setEchoMode(QLineEdit::Password);

    label = new QLabel(this);
    label->setFixedSize(200,50);

    lineEdit1->move(100, 150);
    lineEdit2->move(100, 200);
    label->move(100,250);


    connect(lineEdit1, &QLineEdit::textChanged, this, &Widget::check1);
    connect(lineEdit2, &QLineEdit::textChanged, this, &Widget::check2);

    // 3.用户自定义是否显示密码
    ui->lineEdit_check->setEchoMode(QLineEdit::Password);


}

Widget::~Widget()
{
    delete ui;
}


void Widget::check1()
{
    QString str1 = lineEdit1->text();
    QString str2 = lineEdit2->text();

    if(str1.isEmpty() && str2.isEmpty())
    {
        label->setText("密码为空");
        qDebug() << "密码为空";
    }
    else if(str1 == str2)
    {
        label->setText("两次密码相同");
        qDebug() << "两次密码相同";
    }
    else if(str1 != str2)
    {
        label->setText("两次密码不同");
        qDebug() << "两次密码不同";
    }
}

void Widget::check2()
{
    QString str1 = lineEdit1->text();
    QString str2 = lineEdit2->text();

    if(str1.isEmpty() && str2.isEmpty())
    {
        label->setText("密码为空");
        qDebug() << "密码为空";
    }
    else if(str1 == str2)
    {
        label->setText("两次密码相同");
        qDebug() << "两次密码相同";
    }
    else if(str1 != str2)
    {
        label->setText("两次密码不同");
        qDebug() << "两次密码不同";
    }
}

void Widget::on_lineEdit_textEdited(const QString &arg1)
{
    QString content = arg1;

    int pos = 0;
    if(ui->lineEdit->validator()->validate(content, pos) == QValidator::Acceptable)
    {
        ui->pushButton->setEnabled(true);
    }
    else
    {
        ui->pushButton->setEnabled(false);
    }
}

void Widget::on_checkBox_toggled(bool checked)
{
    if(checked)
    {
        ui->lineEdit_check->setEchoMode(QLineEdit::Normal);
    }
    else
    {
        ui->lineEdit_check->setEchoMode(QLineEdit::Password);
    }
}
